#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <thread>
#include <unistd.h>
#include "task.h"
#include "user.h"
#include "function.h" 

using namespace std;

void help() {
    cout << "Help:\n"
         << "addtask: Write a new task\n"
         << "showtask: Show all tasks\n"
         << "deltask: Delete a task\n"
         << "run: Keep running the program\n";
}
void runhelp() {
    cout << "Interactive Mode Help:\n"
         << "-h: Show this help\n"
         << "-w: Write a new task\n"
         << "-s: Save all changes\n"
         << "-d: Delete a task\n"
         << "-o: Show all tasks\n"
         << "-e: Exit\n";
}
void loadAllUsers(vector<User>& users) {
    ifstream userFile("users.dat");
    if(!userFile) return;
    
    string line;
    while(getline(userFile, line)) {
        size_t pos = line.find(',');
        if(pos == string::npos) continue;
        
        User user;
        user.name = line.substr(0, pos);
        user.password = line.substr(pos + 1);
        user.task = nullptr;
        users.push_back(user);
    }
}

int main(int argc, char* argv[]) {
    vector<User> users;
    loadAllUsers(users);
    User currentUser{"", "", nullptr};
    
    if(argc < 2) {
        help();
        return 1;
    }
    
    string command = argv[1];
    
    if(command == "help") {
        help();
    } 
    else if(command == "run") {
        if(users.empty()) {
            cout << "No users available. Please sign up first.\n";
            return 1;
        }
        
        if(trylog(users, currentUser)) {
            return 0;
        }
        
        thread reminder(reminderThread, &currentUser);
        reminder.detach();
        
        bool end = false;
        runhelp();
        while(!end) {
            string option;
            cout << "> ";
            cin >> option;
            
            if(option == "-h") {
                runhelp();
            }
            else if(option == "-w") {
                Task newtask;
                string group, priority;
                
                cout << "Task Name: ";
                cin.ignore();
                getline(cin, newtask.name);
                
                cout << "Start Time (YYYY-MM-DD HH:MM): ";
                getline(cin, newtask.start);
                
                cout << "Remind Time (YYYY-MM-DD HH:MM): ";
                getline(cin, newtask.remindTime);
                
                cout << "Priority (low/medium/high): ";
                cin >> priority;
                if(priority == "low") newtask.priority = 0;
                else if(priority == "medium") newtask.priority = 1;
                else if(priority == "high") newtask.priority = 2;
                else newtask.priority = 0;
                
                cout << "Group (study/fun/life): ";
                cin >> group;
                if(group == "study") newtask.group = 1;
                else if(group == "fun") newtask.group = 2;
                else if(group == "life") newtask.group = 3;
                else newtask.group = 0;
                
                addTask(currentUser, newtask); 
                saveUserTasks(currentUser);
            }
            else if(option == "-o") {
                show(currentUser);
            }
            else if(option == "-d") {
                string id;
                cout << "Enter Task ID to delete: ";
                cin >> id;
                del(currentUser, id);
                saveUserTasks(currentUser);
            }
            else if(option == "-s") {
                saveUserTasks(currentUser);
                cout << "Tasks saved!\n";
            }
            else if(option == "-e") {
                end = true;
            }
            else {
                cout << "Invalid option. Type -h for help.\n";
            }
        }
    }
    else if(command == "addtask") {
        if(argc < 4) {
            cout << "Usage: " << argv[0] << " addtask username password\n";
            return 1;
        }
        
        string username = argv[2];
        string password = argv[3];
        
        currentUser = login(username, password, users);
        if(currentUser.name == "") {
            return 1;
        }
        
        Task newtask;
        cout << "Task Name: ";
        cin.ignore();
        getline(cin, newtask.name);
        
        cout << "Start Time (YYYY-MM-DD HH:MM): ";
        getline(cin, newtask.start);
        
        cout << "Remind Time (YYYY-MM-DD HH:MM): ";
        getline(cin, newtask.remindTime);
        
        string priority;
        cout << "Priority (low/medium/high): ";
        cin >> priority;
        if(priority == "low") newtask.priority = 0;
        else if(priority == "medium") newtask.priority = 1;
        else if(priority == "high") newtask.priority = 2;
        else newtask.priority = 0;
        
        string group;
        cout << "Group (study/fun/life): ";
        cin >> group;
        if(group == "study") newtask.group = 1;
        else if(group == "fun") newtask.group = 2;
        else if(group == "life") newtask.group = 3;
        else newtask.group = 0;
        
        addTask(currentUser, newtask);  
        saveUserTasks(currentUser);
        cout << "Task added successfully!\n";
    }
    else if(command == "showtask") {
        if(argc < 4) {
            cout << "Usage: " << argv[0] << " showtask username password\n";
            return 1;
        }
        
        string username = argv[2];
        string password = argv[3];
        
        currentUser = login(username, password, users);
        if(currentUser.name == "") {
            return 1;
        }
        
        show(currentUser);
    }
    else if(command == "deltask") {
        if(argc < 5) {
            cout << "Usage: " << argv[0] << " deltask username password taskid\n";
            return 1;
        }
        
        string username = argv[2];
        string password = argv[3];
        string taskid = argv[4];
        
        currentUser = login(username, password, users);
        if(currentUser.name == "") {
            return 1;
        }
        
        del(currentUser, taskid);
        saveUserTasks(currentUser);
    }
    else if(command == "signup") {
        if(argc < 4) {
            cout << "Usage: " << argv[0] << " signup username password\n";
            return 1;
        }
        
        string username = argv[2];
        string password = argv[3];
        signup(username, password, users);
    }
    else {
        cout << "Unknown command. Type 'help' for usage.\n";
        return 1;
    }
    
    return 0;
}
